package querysrc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.subway.babynote.syncdata.*;
import com.subway.babynote.syncdata.ClientDataRecord.food_defaultRecord;
import com.subway.babynote.syncdata.ClientDataRecord.food_uxRecord;
import com.subway.babynote.syncdata.ClientDataRecord.hwAvgRecord;
import com.subway.babynote.syncdata.ClientDataRecord.hwdefaultRecord;
import com.subway.babynote.syncdata.ClientDataRecord.uxRecord;


public abstract class query_height_weight extends query {
	String sqlscript = "";
	public query_height_weight(sqlConnectionHandler sqlconhandler)
	{
		super(sqlconhandler);
	}	
	
	public abstract boolean init_script();
	
	public abstract boolean setParam(int user_id, PreparedStatement ps);
	
	public abstract ClientDataRecordBase getdata(ResultSet rs);
	
	protected uxRecord get_ux(ResultSet rs)
	{
		uxRecord ux = new uxRecord();
		try
		{
			ux.min = rs.getFloat(sqlscripts.sql_get_hw_min);
			ux.max = rs.getFloat(sqlscripts.sql_get_hw_max);
			ux.ts = rs.getInt(sqlscripts.sql_get_hw_ts);
			ux.l.add(rs.getFloat(sqlscripts.sql_get_hw_d1));
			ux.l.add(rs.getFloat(sqlscripts.sql_get_hw_d2));
			ux.l.add(rs.getFloat(sqlscripts.sql_get_hw_d3));
			ux.l.add(rs.getFloat(sqlscripts.sql_get_hw_d4));
			ux.l.add(rs.getFloat(sqlscripts.sql_get_hw_d5));
			return ux;
		}
		catch(SQLException se) {
		      System.out.println("SQLException: "+se.getMessage());
		      return ux;
	    }		
	}
	
	protected hwAvgRecord get_hw_avg(ResultSet rs)
	{
		hwAvgRecord hwar = new hwAvgRecord();
		try
		{
			hwar.age = rs.getInt(sqlscripts.sql_get_hw_age);
			hwar.gender = rs.getBoolean(sqlscripts.sql_get_hw_gender);
			hwar.min = rs.getFloat(sqlscripts.sql_get_min);
			hwar.mid = rs.getFloat(sqlscripts.sql_get_mid);
			hwar.max = rs.getFloat(sqlscripts.sql_get_max);
			hwar.ts = rs.getInt(sqlscripts.sql_get_hw_ts);
			return hwar;
		}
		catch(SQLException se) {
		      System.out.println("SQLException: "+se.getMessage());
		      return hwar;
	    }	
	}
	
	protected hwdefaultRecord get_hwdefault(ResultSet rs)
	{
		hwdefaultRecord hwr = new hwdefaultRecord();
		try
		{
			hwr.age = rs.getInt(sqlscripts.sql_get_hw_age);
			hwr.gender = rs.getBoolean(sqlscripts.sql_get_hw_gender);
			hwr.ux = this.get_ux(rs);
			return hwr;
		}
		catch(SQLException se) {
		      System.out.println("SQLException: "+se.getMessage());
		      return hwr;
	    }	
	}
	
	protected food_uxRecord get_fux(ResultSet rs)
	{
		food_uxRecord fux = new food_uxRecord();
		try
		{
			fux.food_id = rs.getInt(sqlscripts.sql_get_food_id);
			fux.ux = this.get_ux(rs);
			return fux;
		}
		catch(SQLException se) {
		      System.out.println("SQLException: "+se.getMessage());
		      return fux;
	    }	
	}
	
	protected food_defaultRecord get_food_default(ResultSet rs)
	{
		food_defaultRecord fdr = new food_defaultRecord();
		try
		{
			fdr.age = rs.getInt(sqlscripts.sql_get_hw_age);
			fdr.gender = rs.getBoolean(sqlscripts.sql_get_hw_gender);
			fdr.fux = this.get_fux(rs);
			return fdr;
		}
		catch(SQLException se) {
		      System.out.println("SQLException: "+se.getMessage());
		      return fdr;
	    }	
	}
	
	public List<ClientDataRecordBase> get_height_weight(int user_id)
	{
		Connection con = null;	
		ResultSet rs = null;
		querydb qdb = null;
		ArrayList<ClientDataRecordBase> hws = new ArrayList<ClientDataRecordBase>();
		try
		{
			boolean bret = true;
			con = this.init();	
			if(bret)
			{
				qdb = new querydb(con,this.sqlscript);		
				bret = qdb.prePS();
			}
			if(bret)
				bret = this.setParam(user_id, qdb.ps);
			if(bret)
			{				
				rs = qdb.execPSQuery();
				if(rs.next())
				{
					rs.beforeFirst();
					while(rs.next())
					{
						ClientDataRecordBase crb = this.getdata(rs);
						hws.add(crb);												
					}					
				}
			}			
			return hws;
		}
		catch(SQLException se) {
		      System.out.println("SQLException: "+se.getMessage());
		      return hws;
	    }
		catch(Exception e)
		{
			e.printStackTrace();
			return hws;
		}
		finally
		{
			this.release_resultset(rs);
			this.release_querydb(qdb);
			this.release_connection(con);
			this.finalize();
		}
	}
}
